<template>
    <div class="app-container">
        <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="68px"
        >
            <el-form-item label="企业名称" prop="name">
                <el-input
                    v-model="queryParams.name"
                    placeholder="请输入企业名称"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="地址" prop="address">
                <el-input
                    v-model="queryParams.address"
                    placeholder="请输入地址"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="联系电话" prop="tel">
                <el-input
                    v-model="queryParams.tel"
                    placeholder="请输入联系电话"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="是否高新" prop="ifNew">
                <el-select
                    v-model="queryParams.ifNew"
                    placeholder="请选择是否高新"
                    clearable
                >
                    <el-option
                        v-for="dict in dict.type.sys_yes_no"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="年度" prop="year">
                <el-input
                    v-model="queryParams.year"
                    placeholder="请输入年度"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="进出口" prop="ifFoign">
                <el-select
                    v-model="queryParams.ifFoign"
                    placeholder="请选择是否进出口"
                    clearable
                >
                    <el-option
                        v-for="dict in dict.type.sys_yes_no"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    size="mini"
                    @click="handleQuery"
                    >查询</el-button
                >
                <el-button
                    icon="el-icon-refresh"
                    size="mini"
                    @click="resetQuery"
                    >重置</el-button
                >
            </el-form-item>
        </el-form>

        <TableCard>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button
                        type="primary"
                        plain
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                        v-hasPermi="['t:enterprise:add']"
                        >新增
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="success"
                        plain
                        icon="el-icon-edit"
                        size="mini"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['t:enterprise:edit']"
                        >修改
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="danger"
                        plain
                        icon="el-icon-delete"
                        size="mini"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['t:enterprise:remove']"
                        >删除
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="warning"
                        plain
                        icon="el-icon-download"
                        size="mini"
                        @click="handleExport"
                        v-hasPermi="['t:enterprise:export']"
                        >导出
                    </el-button>
                </el-col>
                <right-toolbar
                    :showSearch.sync="showSearch"
                    @queryTable="getList"
                ></right-toolbar>
            </el-row>

            <el-table
                :border="Global.tableShowBorder"
                v-loading="loading"
                :data="enterpriseList"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="企业名称" align="center" prop="name" />
                <el-table-column label="地址" align="center" prop="address" />
                <el-table-column label="联系电话" align="center" prop="tel" />
                <el-table-column
                    label="产值"
                    align="center"
                    prop="productAmt"
                />
                <el-table-column label="税收" align="center" prop="tax" />
                <el-table-column label="是否高新" align="center" prop="ifNew">
                    <template slot-scope="scope">
                        <dict-tag
                            :options="dict.type.sys_yes_no"
                            :value="scope.row.ifNew"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="年度" align="center" prop="year" />
                <el-table-column
                    label="是否进出口"
                    align="center"
                    prop="ifFoign"
                >
                    <template slot-scope="scope">
                        <dict-tag
                            :options="dict.type.sys_yes_no"
                            :value="scope.row.ifFoign"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="进口总额" align="center" prop="inAmt" />
                <el-table-column
                    label="出口总额"
                    align="center"
                    prop="outAmt"
                />
                <el-table-column
                    label="所属园区"
                    align="center"
                    prop="fromPlace"
                >
                    <template slot-scope="scope">
                        {{
                            util.formatLabelByValue(
                                placeList,
                                scope.row.fromPlace,
                                "value",
                                "label"
                            )
                        }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="企业人数"
                    align="center"
                    prop="personCount"
                />
                <el-table-column label="备注" align="center" prop="remark" />
                <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding fixed-width"
                >
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['t:enterprise:edit']"
                            >修改
                        </el-button>
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['t:enterprise:remove']"
                            >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination
                v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
            />
        </TableCard>

        <!-- 添加或修改企业对话框 -->
        <el-dialog
            :title="title"
            :visible.sync="open"
            width="1000px"
            append-to-body
        >
            <TableCard>
                <el-form
                    ref="form"
                    :model="form"
                    :rules="rules"
                    label-width="120px"
                >
                    <el-form-item label="企业名称" prop="name">
                        <el-input
                            v-model="form.name"
                            placeholder="请输入企业名称"
                        />
                    </el-form-item>
                    <el-form-item label="地址" prop="address">
                        <el-input
                            v-model="form.address"
                            placeholder="请输入地址"
                        />
                    </el-form-item>
                    <el-form-item label="联系电话" prop="tel">
                        <el-input
                            v-model="form.tel"
                            placeholder="请输入联系电话"
                        />
                    </el-form-item>
                    <el-form-item label="产值" prop="productAmt">
                        <el-input
                            v-model="form.productAmt"
                            placeholder="请输入产值"
                        />
                    </el-form-item>
                    <el-form-item label="税收" prop="tax">
                        <el-input v-model="form.tax" placeholder="请输入税收" />
                    </el-form-item>
                    <el-form-item label="是否高新" prop="ifNew">
                        <el-select
                            v-model="form.ifNew"
                            placeholder="请选择是否高新"
                        >
                            <el-option
                                v-for="dict in dict.type.sys_yes_no"
                                :key="dict.value"
                                :label="dict.label"
                                :value="dict.value"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="年度" prop="year">
                        <el-input
                            v-model="form.year"
                            placeholder="请输入年度"
                        />
                    </el-form-item>
                    <el-form-item label="是否进出口" prop="ifFoign">
                        <el-select
                            v-model="form.ifFoign"
                            placeholder="请选择是否进出口"
                        >
                            <el-option
                                v-for="dict in dict.type.sys_yes_no"
                                :key="dict.value"
                                :label="dict.label"
                                :value="dict.value"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item
                        label="进口总额"
                        prop="inAmt"
                        v-if="form.ifFoign === 'Y'"
                    >
                        <el-input
                            v-model="form.inAmt"
                            placeholder="请输入进口总额"
                        />
                    </el-form-item>
                    <el-form-item
                        label="出口总额"
                        prop="outAmt"
                        v-if="form.ifFoign === 'Y'"
                    >
                        <el-input
                            v-model="form.outAmt"
                            placeholder="请输入出口总额"
                        />
                    </el-form-item>
                    <el-form-item label="所属园区" prop="fromPlace">
                        <el-select
                            v-model="form.fromPlace"
                            placeholder="请选择所属园区"
                        >
                            <el-option
                                v-for="p in placeList"
                                :key="p.value"
                                :label="p.label"
                                :value="p.value"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="企业人数" prop="personCount">
                        <el-input
                            v-model="form.personCount"
                            placeholder="请输入企业人数"
                        />
                    </el-form-item>
                    <el-form-item label="备注" prop="remark">
                        <el-input
                            v-model="form.remark"
                            type="textarea"
                            :rows="5"
                            placeholder="请输入内容"
                        />
                    </el-form-item>
                </el-form>
            </TableCard>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listEnterprise,
    getEnterprise,
    delEnterprise,
    addEnterprise,
    updateEnterprise,
} from "@/api/t/enterprise";

export default {
    name: "Enterprise",
    dicts: ["sys_yes_no"],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示查询条件
            showSearch: true,
            // 总条数
            total: 0,
            // 企业表格数据
            enterpriseList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                orderByColumn: "createTime",
                isAsc: "descending",
                name: null,
                address: null,
                tel: null,
                productAmt: null,
                tax: null,
                ifNew: null,
                year: null,
                ifFoign: null,
                inAmt: null,
                outAmt: null,
                fromPlace: null,
                personCount: null,
                tenantId: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {},
            placeList: [
                { value: "0", label: "绿色低碳新材料产业园" },
                { value: "1", label: "数字经济产业园" },
                { value: "2", label: "电子信息装备制造产业园" },
                { value: "3", label: "高端装备制造园" },
                { value: "4", label: "轻工业产业园" },
            ],
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询企业列表 */
        getList() {
            this.loading = true;
            listEnterprise(this.queryParams).then((response) => {
                this.enterpriseList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                name: null,
                address: null,
                tel: null,
                productAmt: null,
                tax: null,
                ifNew: null,
                year: null,
                ifFoign: null,
                inAmt: null,
                outAmt: null,
                fromPlace: null,
                personCount: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null,
                tenantId: null,
            };
            this.resetForm("form");
        },
        /** 查询按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加企业";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getEnterprise(id).then((response) => {
                this.form = response.data;
                this.open = true;
                this.title = "修改企业";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateEnterprise(this.form).then((response) => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addEnterprise(this.form).then((response) => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm('是否确认删除企业编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delEnterprise(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                "t/enterprise/export",
                {
                    ...this.queryParams,
                },
                `enterprise_${new Date().getTime()}.xlsx`
            );
        },
    },
};
</script>
